<?php

namespace Worker\Controller;

use Common\Service\ChuanglanSmsTpl;
use Common\Service\Validator;
use Think\Log;

/**
 * 师傅
 * Class WorderControllerWo
 * @package Home\Controller
 */
class WorkerController extends BaseController
{
    public function __construct()
    {
    }

    /**
     * 发送验证码
     */
    public function sendCode()
    {
        $phone = I('phone');
        if (!Validator::checkPhone($phone)) {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '请填写正确的手机号', 'data' => []]);
        }

        $res = ChuanglanSmsTpl::sendCode($phone);
        if ($res) {
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '发送验证码成功', 'data' => []]);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '发送验证码失败', 'data' => []]);
        }
    }

    /**
     * 特长 1=>车身广告安装 2=>店铺广告安装 3=>标识标牌
     * 注册
     */
    public function register()
    {
        $phone = I('phone');
        $password = I('password');
        $sms_code = I('sms_code');
        $invite_code = I('invite_code');
        $specialties = I('specialties');
        $openid = I('openid');

        if (!$phone || !$password || !$sms_code || !$invite_code || !$specialties) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '请填写完整数据再注册', 'data' => []]);
        }

        if (!Validator::checkPhone($phone)) {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '请填写正确的手机号', 'data' => []]);
        }

        $partner = M('partner')->where(['invite_code' => $invite_code])->find();
        if (!$partner) {
            $this->ajaxReturn(['status' => false, 'code' => 1003, 'msg' => '请填写正确的邀请码', 'data' => []]);
        }

        $worker = M('worker')->where(['mobile' => $phone])->find();
        if ($worker) {
            $this->ajaxReturn(['status' => false, 'code' => 1004, 'msg' => '该手机号已被注册', 'data' => []]);
        }

        $scode = S($phone);
        Log::write('师傅注册缓存验证码-用户传过来的验证码：' . $scode . '---' . $sms_code);
        if ($sms_code && $sms_code == $scode) {
            $data = [
                'mobile' => $phone,
                'password' => $password,
                'specialty' => $specialties,
                'partner_id' => $partner['id'],
                'openid' => $openid,
                'add_time' => time()
            ];

            $rs = M('worker')->add($data);
            if ($rs !== false) {
                $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '注册成功', 'data' => ['id' => $rs, 'partner_name' => $partner['name']]]);
            } else {
                $this->ajaxReturn(['status' => false, 'code' => 1006, 'msg' => '服务器异常', 'data' => []]);
            }
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1005, 'msg' => '验证码有误', 'data' => []]);
        }
    }

    /**
     * 登录
     */
    public function login()
    {
        $mobile = I('mobile', '', 'trim');
        $password = I('password', '', 'trim');
        $openid = I('openid', '', 'trim');

        if (!Validator::checkPhone($mobile)) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '手机号错误']);
        }

        if ($password == '') {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '请输入密码']);
        }

        if ($openid == '') {
            $this->ajaxReturn(['status' => false, 'code' => 1003, 'msg' => '非法登录']);
        }

        $worker = $this->chenkLogin('worker', $mobile, $password);

        if (!$worker) {
            $this->ajaxReturn(['status' => false, 'code' => 1004, 'msg' => '账号密码错误']);
        }
        if ($worker) {
            $userInfo = $worker;
        }

        $ret = ['status' => true, 'code' => 1000, 'msg' => '登陆成功', 'data' => $userInfo];
        $this->ajaxReturn($ret);
    }

    /**
     * 认证
     */
    public function authentication()
    {
        $id = I('id');
        $name = I('name');
        $cert_id = I('cert_id');
        $province = I('province');
        $city = I('city');
        $area = I('area');
        $address = I('address');
        $service_area = I('service_area');

        if (!$id || !$name || !$cert_id || !$city || !$province || !$service_area) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '请填写完整数据再认证', 'data' => []]);
        }

        $work = $this->checkUser('worker', $id);
        if (!$work) {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '用户信息有误', 'data' => []]);
        }

        $data = [
            'name' => $name,
            'certid' => $cert_id,
            'province' => $province,
            'city' => $city,
            //'area' => $area,
            //'address' => $address,
            'service_area' => $service_area,
            'update_time' => time()
        ];

        $rs = M('worker')->where(['id' => $id])->save($data);
        if ($rs !== false) {
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '提交申请成功', 'data' => []]);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1003, 'msg' => '服务器异常', 'data' => []]);
        }
    }

    /**
     * 修改手机号
     */
    public function updPhone()
    {
        $ori_phone = I('ori_phone');
        $new_phone = I('new_phone');
        $code = I('code');

        if (!Validator::checkPhone($ori_phone)) {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '请填写正确的原始手机号', 'data' => []]);
        }
        if (!Validator::checkPhone($new_phone)) {
            $this->ajaxReturn(['status' => false, 'code' => 1003, 'msg' => '请填写正确的新手机号', 'data' => []]);
        }

        $info = M('worker')->where(['mobile' => $ori_phone])->find();
        if (!$info) {
            $this->ajaxReturn(['status' => false, 'code' => 1004, 'msg' => '该手机号尚未注册', 'data' => []]);
        }

        $scode = S($new_phone);
        if ($code && $code == $scode) {
            $rs = M('worker')->where(['mobile' => $ori_phone])->save(['mobile' => $new_phone, 'update_time' => time()]);
            if ($rs !== false) {
                $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '修改手机号成功', 'data' => []]);
            } else {
                $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '服务器异常', 'data' => []]);
            }
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1005, 'msg' => '验证码有误', 'data' => []]);
        }
    }

    /**
     * 修改密码
     */
    public function updPassword()
    {
        $ori_pwd = I('ori_pwd');
        $new_pwd = I('new_pwd');
        $conf_pwd = I('conf_pwd');
        $uid = I('id');

        if ($new_pwd != $conf_pwd) {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '两次密码不一致', 'data' => []]);
        }

        if ($new_pwd == $ori_pwd) {
            $this->ajaxReturn(['status' => false, 'code' => 1005, 'msg' => '新密码不能跟原密码相同', 'data' => []]);
        }

        $user = $this->checkUser('worker', $uid);
        if ($user) {
            if ($ori_pwd == $user['password']) {
                $rs = M('worker')->where(['id' => $uid])->save(['password' => $new_pwd, 'update_time' => time()]);
                if ($rs !== false) {
                    $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '修改密码成功', 'data' => []]);
                } else {
                    $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '服务器异常', 'data' => []]);
                }
            } else {
                $this->ajaxReturn(['status' => false, 'code' => 1003, 'msg' => '原始密码错误', 'data' => []]);
            }
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1004, 'msg' => '用户信息错误', 'data' => []]);
        }
    }

    /**
     * 修改个人资料
     */
    public function updInfo()
    {
        $id = I('id');
        $type = I('type', 1);//1查询，2更新
        $user = $this->checkUser('worker', $id);

        if (!$user)
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '用户信息有误']);

        if ($type == 1) {
            $user['sex'] = $user['sex'] == 1 ? '男' : '女';
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '用户信息', 'data' => ['id' => $user]]);
        } else {
            $data = [];
            if (I('age'))
                $data['age'] = I('age');
            if (I('sex'))
                $data['sex'] = I('sex');
            if (I('headimg'))
                $data['headimg'] = I('headimg');
            if (I('name'))
                $data['name'] = I('name');
            if (I('certid'))
                $data['certid'] = I('certid');
            if (I('province'))
                $data['province'] = I('province');
            if (I('service_area'))
                $data['service_area'] = I('service_area');

            if ($data) {
                $res = M('worker')->where(['id' => $id])->save($data);
                if ($res !== false) {
                    $ret = $this->checkUser('worker', $id);
                    $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '更新用户数据成功', 'data' => ['id' => $ret]]);
                } else {
                    $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '服务器异常', 'data' => []]);
                }
            }
        }
    }

    /**
     * 上传所需要的token，domain
     */
    public function uploadInfo()
    {
        $num = I('num');
        $data = Qiniu::getUpimageInfo($num);
        $ret = [
            'status' => true,
            'code' => 1000,
            'msg' => '',
            'data' => $data
        ];

        $this->ajaxReturn($ret);
    }
}